var express = require('express');
var router = express.Router();
var db = require('../bin/db');

router.get('/', function (req, res, next) {
    var authkey = req.query.authkey;
    db.pool.getConnection(function (err, client) {
        if (err){
            res.send(500)
        }else {
            var sql = "SELECT id,name,Language,app_open,weChat_open,email_open,tel_open,Relation_mail1,Relation_mail2,Relation_mail3,Relation_mail4,Relation_mail5,Relation_mail_type_1,Relation_mail_type_2,Relation_mail_type_3,Relation_mail_type_4,Relation_mail_type_5,verifi_mail from users where authkey = '" + authkey + "'";
            client.query(sql, function (err, rows) {
                if (err){
                    res.send(500)
                }else {
                    var Result = rows[0];
                    if (Result) {
                        var Language_js;
                        var Language = Result.Language;
                        if (Language == "zh_FT"){
                            Language_js = 'zh_ft.js';
                        }else if (Language == "ru"){
                            Language_js = 'ru.js'
                        }else if (Language == "ko"){
                            Language_js = 'ko.js'
                        }else if (Language == "en"){
                            Language_js = 'en.js'
                        }else if (Language == "de"){
                            Language_js = 'de.js'
                        }else if (Language == "es"){
                            Language_js = 'es.js'
                        }else if (Language == "it"){
                            Language_js = 'it.js'
                        }else if (Language == "fr"){
                            Language_js = 'fr.js'
                        }else if (Language == "ja"){
                            Language_js = 'ja.js'
                        }else if (Language == "zh"){
                            Language_js = 'zh.js'
                        }

                        var data = {
                            "Language":Language,
                            "Language_js": Language_js,
                            "verifi_mail":Result.verifi_mail,
                            "Relation_mail1":Result.Relation_mail1,
                            "Relation_mail2":Result.Relation_mail2,
                            "Relation_mail3":Result.Relation_mail3,
                            "Relation_mail4":Result.Relation_mail4,
                            "Relation_mail5":Result.Relation_mail5,
                            "Relation_mail_type_1":Result.Relation_mail_type_1,
                            "Relation_mail_type_2":Result.Relation_mail_type_2,
                            "Relation_mail_type_3":Result.Relation_mail_type_3,
                            "Relation_mail_type_4":Result.Relation_mail_type_4,
                            "Relation_mail_type_5":Result.Relation_mail_type_5
                        };
                        console.log(data);
                        res.render('email',data)
                    }
                }

            })
        }

    })
});

module.exports = router;
